<?php
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/7/15
 * Time: 上午10:17
 */

namespace Hilaoyu\Utils;

/**
 * @method static string patternMobile() 返回手机号码正则表达式
 * @method static string patternUuid() 返回uuid正则表达式
 * @method static string patternEmail() 返回email正则表达式
 * @method static string patternAccount() 返回email正则表达式
 * @method static bool isMobile(string $mobile) 是否为手机号码
 * @method static bool isUuid(string $uuid) 是否为uuid
 * @method static bool isEmail(string $email) 是否为email
 * @method static bool isAccount(string $email) 是否为email
 */

class UtilRegex
{
    static protected $_pattern = [
        'account' => '^[a-z0-9][a-z0-9_]*[a-z0-9]$',
        'mobile' => '^(\+?[\d]{2,3})?1[3456789]{1}\d{9}$',
        'uuid' => '^[0-9A-Za-z]{8}-?[0-9A-Za-z]{4}-?[0-9A-Za-z]{4}-?[0-9A-Za-z]{4}-?[0-9A-Za-z]{12}$',
        'email' => '^\w+([-\.]\w+)*@\w+([\.-]\w+)*\.\w{2,4}$',
    ];


    static public function pattern($key){
        return array_key_exists($key,static::$_pattern) ? static::$_pattern[$key] : '';
    }

    static public function is($key,$str){
        if(! $pattern = static::pattern($key)){
            return false;
        }
        if(!is_string($str)){
            return false;
        }

        return (bool)preg_match('/'. $pattern.'/',$str);

    }

    static public function __callStatic($name, $arguments)
    {
        $name = UtilStr::humpToUnderline($name);

        if('pattern' == UtilStr::before($name,'_')){
            return static::pattern(UtilStr::substr($name,8));
        }elseif('is' == UtilStr::before($name,'_')){
            return static::is(UtilStr::substr($name,3),...$arguments);
        }

        throw new \BadMethodCallException();

    }

}
